package com.zline.foodraise.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zline.foodraise.R;
import com.zline.foodraise.adapter.CommentListItemPicsAdapter;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.CommentBean;
import com.zline.foodraise.entity.CustomerUserBean;
import com.zline.foodraise.entity.PictureBean;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.ImageUtil;
import com.zline.foodraise.util.StringUtils;
import com.zline.foodraise.util.TimeUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.List;

/**
 * Created by heshantao on 15/7/15.
 * 评论详情页中每个评论Item
 */
@EViewGroup(R.layout.detail_dynamic_comment_item)
public class DetailDynamicCommentItem extends RelativeLayout {
    private static final String LOG_TAG = DetailDynamicCommentItem.class.getSimpleName();
    Context cxt;
    RecyclerItemClicked itemClicked;
    @ViewById(R.id.comment_person_icon_circleIv)
    CircleImageView iv;
    @ViewById(R.id.detail_comment_list_item_vip)
    ImageView vip_iv;
    @ViewById(R.id.nickName_tv)
    TextView nickeName_tv;
    @ViewById(R.id.comment_tv)
    TextView comment_tv;
    @ViewById(R.id.comment_time_tv)
    TextView time_tv;
    @ViewById(R.id.dynamic_comment_pics_recyclerView)
    RecyclerView picRecyclerView;
    @Bean
    CommentListItemPicsAdapter picsAdapter;
    SparseArray<String> picsSparse;
    @Bean
    ImageUtil imageUtil;
    DisplayImageOptions options;
    ImageLoader imageLoader;
    String picUrl;
    int identificateType;
    int position;
    @StringRes(R.string.reply)
    String reply;
    @StringRes(R.string.today)
    String today;
    @Bean
    TimeUtil timeUtil;

    public DetailDynamicCommentItem(Context context) {
        super(context);
        this.cxt=context;
    }

    public void bind(CommentBean commentBean, int position,DisplayImageOptions options) {
        this.position = position;
        imageLoader = ImageLoader.getInstance();
        CustomerUserBean customerUserBean = commentBean.getUserBean();
        if (customerUserBean == null) return;
        picUrl = customerUserBean.getAvatarPicUrl();
        imageLoader.displayImage(picUrl, iv, options);
        identificateType=customerUserBean.getIdentificateType();
        if(identificateType==1){
            vip_iv.setVisibility(VISIBLE);
        }else {
            vip_iv.setVisibility(GONE);
        }

        String name = customerUserBean.getNickName();
        int name_length=name.length();
        String replyUserName = commentBean.getReplyNickName();
        if(StringUtils.isBlank(replyUserName)){
            nickeName_tv.setText(name);
        }else{
            String title=name+reply+replyUserName;
            SpannableString spannableString=new SpannableString(title);
            spannableString.setSpan(new ForegroundColorSpan(Color.BLACK)
                    ,name_length,name_length+2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            nickeName_tv.setText(spannableString);
        }
        String content = commentBean.getContent();
        comment_tv.setText(content);
        long time = commentBean.getCreateTimeStamp();
        String createTime = formatData(time);
        time_tv.setText(createTime);

        List<PictureBean> pictureBeanList = commentBean.getPictureBeanList();
        picsSparse = new SparseArray<String>();
        for (int i = 0; i < pictureBeanList.size(); i++) {
            PictureBean pictureBean = pictureBeanList.get(i);
            picsSparse.put(i, pictureBean.getPictureUrl());
        }
        setPicRecyclerView();

    }

    //将时间戳转化为日期格式
    String formatData(long timeStamp) {
        String result = "";
        if (timeUtil.isToday(timeStamp)) {
            result = today+" "+timeUtil.formatHourAndMin(timeStamp);
        } else {
            result=timeUtil.formatData2(timeStamp);
        }
        return result;
    }

    //设置recyclerView
    void setPicRecyclerView() {
        picRecyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);//去除阴影
        MyGridLayoutManager layoutManager = new MyGridLayoutManager(cxt, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        picRecyclerView.setLayoutManager(layoutManager);
        picsAdapter.setAdapterData(picsSparse);
        picsAdapter.setRecycleItemClickListener(itemClicked);
        picsAdapter.setRecycleFlag(Constant.DETAIL_DYNAMIC_COMMENT_FLAG);
        picsAdapter.setParentPostion(position);
        picRecyclerView.setAdapter(picsAdapter);
        picRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画
    }

    public void setRecyclerItemClick(RecyclerItemClicked clicked) {
        this.itemClicked = clicked;
    }

    @Click({R.id.detail_comment_item_rl,R.id.comment_person_icon_circleIv})
    void onClick(View view) {
        switch (view.getId()){
            case R.id.detail_comment_item_rl:
                itemClicked.onRecyclerItemClicked(Constant.DYNAMIC_COMMENT_FLAG, position);
                break;
            case R.id.comment_person_icon_circleIv:
                itemClicked.onRecyclerItemClicked(Constant.DYNAMIC_THIRD_PERSONAL_FLAG, position);
                break;
            default:
                break;
        }

    }
}
